package com.whale.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.whale.DetailActivity;
import com.whale.PicViewActivity;
import com.whale.R;
import com.whale.WhaleApplication;
import com.whale.constants.WhaleConstants;
import com.whale.entity.Stream;

/**
 * 动态页面的Adapter
 * 
 * 几个时间需要后期填充
 * 
 * @author lzk
 * 
 */
public class TrendListAdapter extends BaseArrayListAdapter<Stream> {

	public TrendListAdapter(Activity context) {
		super(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder;

		if (row == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			row = inflater.inflate(R.layout.trenditem, null);

			holder = new ViewHolder();

			holder.trend_right = (RelativeLayout) row
					.findViewById(R.id.trend_right);

			holder.trendName = (TextView) row.findViewById(R.id.trendName);
			holder.trendTime = (TextView) row.findViewById(R.id.trendTime);
			holder.trendContent = (TextView) row
					.findViewById(R.id.trendContent);
			holder.trendImage = (ImageView) row.findViewById(R.id.trendImage);
			holder.streamImage = (ImageView) row.findViewById(R.id.streamImage);
			holder.trend_comments = (TableLayout) row
					.findViewById(R.id.trend_comments);
			holder.rl_comment_content2 = (RelativeLayout) row
					.findViewById(R.id.rl_comment_content2);
			holder.tr_stream_comments = (TableRow) row
					.findViewById(R.id.tr_stream_comments);

			holder.commentNum = (TextView) row.findViewById(R.id.commentNum);

			holder.comment_name1 = (TextView) row
					.findViewById(R.id.tv_comment_name1);
			holder.comment_date1 = (TextView) row
					.findViewById(R.id.tv_comment_date1);
			holder.comment_content1 = (TextView) row
					.findViewById(R.id.tv_comment_content1);
			holder.comment_name2 = (TextView) row
					.findViewById(R.id.tv_comment_name2);
			holder.comment_date2 = (TextView) row
					.findViewById(R.id.tv_comment_date2);
			holder.comment_content2 = (TextView) row
					.findViewById(R.id.tv_comment_content2);

			row.setTag(holder);
		} else {
			holder = (ViewHolder) row.getTag();
		}

		final Stream stream = list.get(position);

		if (stream.cache_avatar.bitmap != null) {
			holder.trendImage.setImageBitmap(stream.cache_avatar.bitmap);
		} else {
//			WhaleApplication.ImageDLWithCache.download(stream.user.avatar,
//					holder.trendImage, R.drawable.avatar50_50,
//					stream.cache_avatar);
		}

		if (stream.type.equals(WhaleConstants.STREAM_PARAMS_TYPE_PHOTO)) {
			holder.trendContent.setVisibility(View.VISIBLE);
			holder.streamImage.setVisibility(View.VISIBLE);
			holder.trend_comments.setVisibility(View.VISIBLE);

			holder.trendName.getPaint().setFakeBoldText(true);
			holder.trendName.setText(stream.user.name);
			// holder.date.setText(DateUtil
			// .getBBLDateString(streamWrap.stream.gmtCreate));
			holder.trendContent.setText(stream.content);
			if (stream.content.equals("")) {
				// 文字内容为空时，隐藏该栏
				holder.trendContent.setVisibility(View.GONE);
			}
			if (stream.cache_image.bitmap != null) {
				holder.streamImage.setImageBitmap(stream.cache_image.bitmap);
			} else {
//				WhaleApplication.ImageDLWithCache.download(
//						stream.parts.get(0).image, holder.streamImage,
//						R.drawable.img_loading, stream.cache_image);
			}
			final String picUrl = stream.parts.get(0).image;
			holder.streamImage.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, PicViewActivity.class);
					intent.putExtra("picUrl", picUrl);
					context.startActivity(intent);
				}
			});
			holder.trend_right.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, DetailActivity.class);
					intent.putExtra("streamId", stream.id);
					intent.putExtra("publisher", stream.user.uid);
					context.startActivity(intent);
				}
			});
		} else if (stream.type.equals(WhaleConstants.STREAM_PARAMS_TYPE_TEXT)) {
			holder.trendContent.setVisibility(View.VISIBLE);
			holder.streamImage.setVisibility(View.GONE);
			holder.trend_comments.setVisibility(View.VISIBLE);

			holder.trendName.getPaint().setFakeBoldText(true);
			holder.trendName.setText(stream.user.name);
			// holder.date.setText(DateUtil
			// .getBBLDateString(streamWrap.stream.gmtCreate));
			holder.trendContent.setText(stream.content);
			holder.trend_right.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, DetailActivity.class);
					intent.putExtra("streamId", stream.id);
					intent.putExtra("publisher", stream.user.uid);
					context.startActivity(intent);
				}
			});
		} else if (stream.type
				.equals(WhaleConstants.STREAM_PARAMS_TYPE_FRIENDCONN)) {
			holder.trendContent.setVisibility(View.GONE);
			holder.streamImage.setVisibility(View.GONE);
			holder.trend_comments.setVisibility(View.GONE);

			StringBuilder sbName = new StringBuilder();
			sbName.append(stream.streamExtra.fname);
			sbName.append(" 和 ");
			sbName.append(stream.streamExtra.tname);
			sbName.append(" 建立了好友关系");
			holder.trendName.setText(sbName.toString());
			// holder.date.setText(DateUtil
			// .getBBLDateString(streamWrap.stream.gmtCreate));
			holder.trend_right.setOnClickListener(null);
		}

		holder.commentNum.setText("(" + stream.comments.size() + ")");
		// 评论内容，最多2条
		if (stream.comments.size() == 0) { // 无评论,则不显示
			holder.tr_stream_comments.setVisibility(View.GONE);
			holder.rl_comment_content2.setVisibility(View.GONE);

		} else if (stream.comments.size() == 1) {
			holder.tr_stream_comments.setVisibility(View.VISIBLE);
			holder.rl_comment_content2.setVisibility(View.GONE);

			holder.comment_name1.setText(stream.comments.get(0).user.name);
			// holder.comment_date1.setText(DateUtil
			// .getBBLDateString(streamWrap.comments.get(0).gmtCreate));
			holder.comment_content1.setText(stream.comments.get(0).content);
		} else if (stream.comments.size() == 2) {
			holder.tr_stream_comments.setVisibility(View.VISIBLE);
			holder.rl_comment_content2.setVisibility(View.VISIBLE);

			holder.comment_name1.setText(stream.comments.get(1).user.name);
			// holder.comment_date1.setText(DateUtil
			// .getBBLDateString(streamWrap.comments.get(1).gmtCreate));
			holder.comment_content1.setText(stream.comments.get(1).content);
			holder.comment_name2.setText(stream.comments.get(0).user.name);
			// holder.comment_date2.setText(DateUtil
			// .getBBLDateString(streamWrap.comments.get(0).gmtCreate));
			holder.comment_content2.setText(stream.comments.get(0).content);
		}

		return row;
	}

	/**
	 * 
	 * 此类用于对应配置文件中的各个元素，方便利用adapter进行数据填充
	 * 
	 * @author lzk
	 * 
	 */
	static class ViewHolder {
		RelativeLayout trend_right;

		TextView trendName;
		TextView trendTime;
		TextView trendContent;
		ImageView trendImage;
		ImageView streamImage;

		TextView commentNum;
		TableLayout trend_comments;
		TableRow tr_stream_comments;

		RelativeLayout rl_comment_content1;
		RelativeLayout rl_comment_content2;

		TextView comment_name1;
		TextView comment_date1;
		TextView comment_content1;
		TextView comment_name2;
		TextView comment_date2;
		TextView comment_content2;
	}

}
